package com.flashcash.solu.common;

import android.util.Log;

import com.flashcash.solu.app.base.BaseLifeCycleView;
import com.flashcash.solu.bean.LatestLoanAppBean;
import com.flashcash.solu.common.network.ServiceGenerator;
import com.flashcash.solu.common.network.UserApi;
import com.flashcash.solu.widget.xleotoast.XLeoToast;
import com.x.leo.apphelper.data.cache.CacheDataManage;
import com.x.leo.apphelper.log.XLog;

import java.io.IOException;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import okhttp3.ResponseBody;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;


/**
 * @作者:XLEO
 * @创建日期: 2017/9/15 18:27
 * @描述:${TODO}
 * @更新者:${Author}$
 * @更新时间:${Date}$
 * @更新描述:${TODO}
 * @下一步：
 */

public class StateUpdater {
    private static Map<Integer, Long> timeStamp    = Collections.synchronizedMap(new LinkedHashMap<Integer, Long>(2));
    private static List<Subscriber<LatestLoanAppBean>> sSubscribers = Collections.synchronizedList(new LinkedList<Subscriber<LatestLoanAppBean>>());
    private static UserApi userApi = ServiceGenerator.createService(UserApi.class);;

    private static <X extends LatestLoanAppBean> Observable.Transformer<X, X> bindToLifecycle(BaseLifeCycleView view) {
        return view.bindToLifecycle();
    }



    public static void refrashDataState(final BaseLifeCycleView view, final int key, final OnRequestError l){
        if (!TokenManager.getInstance().hasLogin()) {
            if (l != null) {
                l.onError();
            }
            return;
        }
        Observable<LatestLoanAppBean> observable =
                userApi.getLatestLoanApp(TokenManager.getInstance().getToken(), "StateUpDater");

        if (view != null) {
            observable = observable.compose(StateUpdater.bindToLifecycle(view));
        }

        observable
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<LatestLoanAppBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e("DataRefrash","update data state failed",e);
                        if (l != null) {
                            l.onError();
                        }
                    }

                    @Override
                    public void onNext(LatestLoanAppBean latestLoanAppBean) {
                        Log.e("DataRefrash","update data state" + latestLoanAppBean.toString());
                        CacheDataManage.INSTANCE.addData(key,latestLoanAppBean);
                    }
                });
    }

    public static void refrashVersion(final int updateVersonBean, final OnRequestError l) {
        userApi.getVersionInfo().subscribeOn(Schedulers.io())
                .observeOn(Schedulers.newThread())
                .subscribe(new Subscriber<ResponseBody>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        XLog.INSTANCE.e("",e,100);
                        XLeoToast.showMessage(e.getMessage());
                        if (l != null) {
                            l.onError();
                        }
                    }

                    @Override
                    public void onNext(final ResponseBody versionBean) {
                        try {
                            CacheDataManage.INSTANCE.addData(updateVersonBean,Integer.valueOf(versionBean.string()));
                        } catch (IOException e) {
                            Log.e("StateUpdater", "", e);
                        }
                    }
                });
    }

    public interface OnRequestError{
        void onError();
    }
}
